package com.zaofan.controller;

import com.zaofan.bean.Account;
import com.zaofan.bean.Admin;
import com.zaofan.service.AccountService;
import com.zaofan.utils.JsonUtils;
import com.zaofan.utils.WebUtils;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/account")
public class AccountController extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private AccountService accountService = new AccountService();

	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Map<String, Object> params = new HashMap<>();
			Enumeration<String> enumeration = request.getParameterNames();
			while (enumeration.hasMoreElements()) {
				String name = enumeration.nextElement();
				if ("action".equals(name)) {
					continue;
				}
				Object value = request.getParameter(name);
				params.put(name, value);
			}
			Admin admin = (Admin) request.getSession().getAttribute("admin");
			System.out.println("regeistDate:"+params.get("regeistDate"));
			System.out.println("platform:"+params.get("platform"));
			List<Account> citys;
			Long count;
			if (admin.getCityId() != 0) {
				params.put("cityId", admin.getCityId());
				 citys = accountService.findAll(params);
				 count = accountService.count(params);
			}
			else {
				 citys = accountService.findAll(params);
				 count = accountService.count(params);
			}

			
			JsonUtils.writeSuccessPageJson("查询成功", citys, count, response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("参数错误!!", response);
		}
		JsonUtils.writeListFailJson("查询失败!!", response);
	}

	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Account rank = WebUtils.write2bean(request, Account.class);
			accountService.add(rank);
			JsonUtils.writeSuccessJson("添加成功", "", response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("添加失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeListFailJson("添加失败!!", response);
	}

	protected void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Account rank = WebUtils.write2bean(request, Account.class);
			accountService.update(rank);
			JsonUtils.writeSuccessJson("修改成功", "", response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("修改失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeListFailJson("修改失败!!", response);
	}

	protected void get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String id_s = request.getParameter("id");
			Integer id = Integer.valueOf(id_s);
			Account rank = accountService.findById(id);
			JsonUtils.writeSuccessJson("查询成功", rank, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询成功!!" + e.getMessage(), response);
		}
		JsonUtils.writeBeanFailJson("查询成功!!", response);
	}

	protected void disable(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String id_s = request.getParameter("id");
			Integer id = Integer.valueOf(id_s);
			Account account = accountService.findById(id);
			if(account == null){
				JsonUtils.writeFailJson("用户信息错误", response);
				return;
			}
			account.setFreeze("y");
			accountService.update(account);
			JsonUtils.writeSuccessJson("修改成功", "", response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("修改失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeListFailJson("修改失败!!", response);
	}
	
	protected void able(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String id_s = request.getParameter("id");
			Integer id = Integer.valueOf(id_s);
			Account account = accountService.findById(id);
			if(account == null){
				JsonUtils.writeFailJson("用户信息错误", response);
				return;
			}
			account.setFreeze("n");
			accountService.update(account);
			JsonUtils.writeSuccessJson("修改成功", "", response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("修改失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeListFailJson("修改失败!!", response);
	}
	
}
